import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [

    {
      path: '/',
      name: 'home',
      component: HomeView,
      redirect: '/search',
      children: [
        {
          path: 'search',
          name: 'search',
          // route level code-splitting
          // this generates a separate chunk (search.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/SearchView.vue')
        },



      ]
    },
    {
      path: '/',
      name: 'login',
      component: ()=>('../views/LoginView.vue')
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue')
    },
    {
      path: '/mytrip',
      name:'mytrip',
      component: () =>import('../views/MyTirpView.vue')
    },
    {
      path: '/login',
      name:'login',
      component: () =>import('../views/LoginView.vue')
    },
    {
      path: '/pcsuccess',
      name:'pcsuccess',
      component: () =>import('../views/PinCheSuccessView.vue')
    },
    {
      path: '/jpc',
      name:'jpc',
      component: () =>import('../views/JoinPinCheView.vue')
    },
    {
      path: '/createOrder',
      name:'createOrder',
      component: () =>import('../views/CreateOrderView.vue')
    },
    {
      path:'/personal',
      name:'personal',
      component: ()  =>import('../views/PersonalView.vue')
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') // 使用完整路径
    return next();

  //获取token
  const tokenStr = window.localStorage.getItem('token');
  if (!tokenStr) {
    alert('您还没有登录，请先登录');
    return next('/login'); // 使用完整路径
  } else {
    next();
  }
})

export default router
